package com.zb.file.controller;

import com.zb.exception.VendorIdentifierException;
import com.zb.file.service.FileService;
import com.zb.result.Result;
import com.zb.service.ZjCustomerService;
import com.zb.utils.TokenMobileUtil;
import com.zb.utils.TokenUserUtil;
import com.zb.utils.TokenUtil;
import com.zb.yaml.FilePath;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author WEN
 * @Date 2023/11/16 0016
 **/
@Api(tags = "移动端附件上传类接口")
@RestController
@RequestMapping("/mobile/file/annex")
public class MobileAnnexFileController {
    @Resource
    private FileService fileService;
    @Resource
    private ZjCustomerService customerService;

    @ApiOperation("头像上传")
    @PostMapping("/avatarUpload")
    public Result avatarUpload(HttpServletRequest request, @ApiParam(value = "文件，以二进制方式上传（form-data），只能单个上传") MultipartFile file) throws VendorIdentifierException {
        String annexCode = TokenMobileUtil.getAnnexCode(request);
        if (null == annexCode) {
            Long custAccId = TokenUserUtil.getCustAccIdByMobildUserCustCode(request);
            annexCode = customerService.getById(custAccId).getAnnexCode();
        }
        return Result.success("文件上传成功").data("rows",
                fileService.avatarUpload(FilePath.annexUploadPath(), FilePath.domain() + FilePath.annexVisitPath(), annexCode, file));
    }

    @ApiOperation("工单附件上传")
    @PostMapping("/order/single")
    public Result orderAnnexUpload(HttpServletRequest request, @ApiParam(value = "文件，以二进制方式上传（form-data），支持多个上传") MultipartFile file) throws VendorIdentifierException {
        String annexCode = TokenMobileUtil.getAnnexCode(request);
        if (null == annexCode) {
            Long custAccId = TokenUserUtil.getCustAccIdByMobildUserCustCode(request);
            annexCode = customerService.getById(custAccId).getAnnexCode();
        }
        List<MultipartFile> files = new ArrayList<>();
        if (null != file) files.add(file);
        return Result.success("文件上传成功").data("rows",
                fileService.orderAnnexUpload(FilePath.annexUploadPath(),
                        FilePath.domain() + FilePath.annexVisitPath(), annexCode, files));
    }

    @ApiOperation("工单附件上传")
    @PostMapping("/order")
    public Result orderAnnexUpload(HttpServletRequest request, @ApiParam(value = "文件，以二进制方式上传（form-data），支持多个上传") List<MultipartFile> files) throws VendorIdentifierException {
        String annexCode = TokenMobileUtil.getAnnexCode(request);
        if (null == annexCode) {
            Long custAccId = TokenUserUtil.getCustAccIdByMobildUserCustCode(request);
            annexCode = customerService.getById(custAccId).getAnnexCode();
        }
        return Result.success("文件上传成功").data("rows",
                fileService.orderAnnexUpload(FilePath.annexUploadPath(),
                        FilePath.domain() + FilePath.annexVisitPath(), annexCode, files));
    }
}
